package com.xuelang.pipeline.worker.app.namespace.impl;

import com.xuelang.pipeline.worker.app.common.util.K8sUtils;
import com.xuelang.pipeline.worker.app.common.util.VerifyUtil;
import com.xuelang.pipeline.worker.app.namespace.NamespaceService;
import com.xuelang.pipeline.worker.client.common.exception.BizException;
import com.xuelang.pipeline.worker.client.namespace.dto.NamespaceQueryDTO;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.openapi.models.V1NamespaceList;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * @author liuyuan.ly
 * @since 2024/5/16 17:12
 */
@Service
@Slf4j
public class NamespaceServiceImpl implements NamespaceService {
    @Override
    public V1NamespaceList getNamespace(NamespaceQueryDTO queryDTO) {
        K8sUtils.initApiClient(queryDTO.getKubeConfig());
        CoreV1Api api = new CoreV1Api();
        V1NamespaceList v1NamespaceList = null;
        try {
            v1NamespaceList = api.listNamespace(
                    null, null, null, null, null,
                    null, null, null, null, null
            );
        /*} catch (ApiException e) {
            log.info("获取namespace列表信息失败:{},堆栈信息:", e.getResponseBody(), e);
            throw new RuntimeException(e.getResponseBody());
        }*/
        } catch (Exception e) {
            log.error("获取namespace列表信息失败:{},堆栈信息:", e.getMessage(), e);
            throw new BizException("配置集群连接异常，请检查多云集成中所在KubeConfig是否准确。");
        }
        return v1NamespaceList;
    }

    @Override
    public V1Namespace createNamespace(NamespaceQueryDTO queryDTO) {
        VerifyUtil.verifyStringNotBlank(queryDTO.getNamespaceName(), "未获取到命名空间的信息");
        String name = queryDTO.getNamespaceName();
        K8sUtils.initApiClient(queryDTO.getKubeConfig());
        CoreV1Api api = new CoreV1Api();
        V1Namespace body = new V1Namespace();
        V1ObjectMeta meta = new V1ObjectMeta();
        meta.setName(name);
        body.setMetadata(meta);
        V1Namespace namespace = null;
        try {
            V1NamespaceList v1NamespaceList = api.listNamespace(
                    null, null, null, "metadata.name=" + name, null,
                    1, null, null, null, null
            );
            if (!CollectionUtils.isEmpty(v1NamespaceList.getItems())) {
                throw new RuntimeException("namespace为" + name + "已存在");
            }
            namespace = api.createNamespace(body, null, null, null, null);
        } catch (ApiException e) {
            log.info("namespace:{}创建失败\napi返回信息:{},堆栈信息:", queryDTO.getNamespaceName(), e.getResponseBody(), e);
            throw new RuntimeException(e.getResponseBody());
        }
        return namespace;
    }

    @Override
    public Boolean deleteNamespace(NamespaceQueryDTO queryDTO) {
        return null;
    }
}
